#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include "QStack.h"
#include "QStack2.h"

char proceed( char x1, char x2 ){
	char result;
	char midString[2];

	result = '<';
	midString[0] = x2;
	midString[1] = 0;

	if( ( ( x1 == '+' || x1 == '-' ) && strstr( "+-)#", midString ) != NULL ) ||
	    ( ( x1 == '*' || x1 == '/' ) && strstr( "+-*/)#", midString ) != NULL ) ||
	    (  x1 == ')' && strstr( "+-*/)#", midString ) != NULL )
	    )
		result = '>';
	else if( ( x1 == '(' && x2 == ')' ) || ( x1 == '#' && x2 == '#' ) )
		result = '=';
	else if( ( x1 == '(' && x2 == '#' ) || ( x1 == ')' && x2 == '(' ) ||
		 ( x1 == '#' && x2== ')' ) )
		result = ' ';

	return result;
}

float sum( float a, float b, char c ){
       	switch( c ){
	case '+': 
		return  a + b;
	case '-': 
		return  a - b;
	case '*': 
		return  a * b;
	case '/': 
		return  a / b;
	}
}


float floatValue(char a){
      return (float) (a - '0');
}

float parse(char *a){
      float f1 = 0;
      float f2 = 0;
      int flag = 0;
      while(*a != '\0'){
               if( !(*a >= '0' && *a <= '9') && *a != '.' ){
                       printf("illegal string\n");
                       return 0;
               }
               if( *a != '.' ){
                   if( !flag )
                           f1 = f1 * 10 + floatValue(*a);
                   else
                           f2 += floatValue(*a) / pow(10, flag++);
               }else
                    ++flag;
               a++;             
      }    
      return f1 + f2;
}

int getElem( char *s, char *elem ){
	if( strlen( s ) == 0 ){
		elem = NULL;
		return 0;
	}
	int len = 0;
	char *a = s;
	char string[2];

	if( !(*a >= '0' && *a <= '9') ){
		elem[ 0 ] = *a;
		elem[ 1 ] = '\0';
		++a;
		strcpy( s, a );
		return 1;
	}else{
		while(  (*a >= '0' && *a <= '9') || *a == '.' ){
			++len;
			++a;
		}
		strncpy( elem, s, len );
/* 		strcat( elem, "\0" ); */
		strcpy( s, a );
		return 2;
	}
}

float compute( char *s ){
	strcat( s, "#" );
	QStype *qs1 = ( QStype* ) malloc( sizeof( QStype ) );
	if( qs1 == NULL ){
		printf( "space malloc failed!!\n" );
		exit( 0 );
	}
	initiateQStack( qs1 );
       
	QStype2 *qs2 = ( QStype2* ) malloc( sizeof( QStype2 ) );
	if( qs2 == NULL ){
		printf( "space malloc failed!!\n" );
		exit( 0 );
	}
	initiateQStack2( qs2 );

	pushQStack2( qs2, '#' );

	char elem[10];
	memset( elem, 0, 10 );
	int t = 0;
	t = getElem( s, elem );
	while( t != 0  ){
		if( t == 1 ){
			char a = elem[ 0 ];
			char b = 0;
			float m = 0;
			float n = 0;
			switch( proceed( getTopElem2( qs2 ), a ) ){
			case '<':
				pushQStack2( qs2, a );
				t = getElem( s, elem );
				break;
			case '=':
				popQStack2( qs2,  &b);
				t = getElem( s, elem );
				break;
			case '>':
				popQStack2( qs2, &b );
				popQStack( qs1, &m );
				popQStack( qs1, &n );
				pushQStack( qs1, sum(m, n, b ) );
				break;
			case ' ':
				printf( "!!illegal expression!!\n" );
				exit( 0 );
			}
		}else{
			float f = parse( elem );
			printf( "%f\n", f );
			pushQStack( qs1, f );
			t = getElem( s, elem );
		}
	}
	
	float result = 0; 
	popQStack( qs1, &result );
	return result;

}

int test(char *s )
{
	char *a = s;
	++a;
	strcpy( s, a );
	return 1;
}

int main()
{
	char s[ 100 ] = "1.5+2*(1+1.5)";
	float f = compute( s );
	printf( "%f", f );
	
}
